package com.min.gps.web;

import com.min.gps.model.GPSInfo;
import com.min.gps.model.LoraInfo;
import com.min.gps.model.Result;
import com.min.gps.service.DataServer;
import com.min.gps.util.StringUtils;
import io.swagger.annotations.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;

import java.net.URISyntaxException;
import java.util.List;

/**
 * Created by mincx
 */
@RestController
@RequestMapping("/api/DataController")
@Api(value = "", description = "data报文数据解析")
public class DataController extends BaseController {
    Logger log = LogManager.getLogger(DataServer.class);
    @Autowired
    private MongoTemplate mongoTemplate;

    @RequestMapping(value = "/queryLoraData", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "获取Lora数据分页查询", notes = "获取Lora数据分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "integer", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "integer", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public Result queryLoraData(@RequestParam(required = false) @ApiParam("车辆ID") String cityID,
                                // @RequestParam(required = false) @ApiParam("推送时间") String sendTime,
                                @ApiIgnore Pageable pageable) throws URISyntaxException {
        try {
            Query query = new Query();
            if (StringUtils.isNotEmpty(cityID)) {
                query.addCriteria(Criteria.where("cityID").is(cityID));//is 精准查询
            }
//            if (StringUtils.isNotEmpty(sendTime)) {
//                query.addCriteria(Criteria.where("sendTime").gte(sendTime));//.gte大于等于
//            }
            List<LoraInfo> list = mongoTemplate.find(query, LoraInfo.class);
            int total = list.size();
            query.with(pageable);
            list = mongoTemplate.find(query, LoraInfo.class);
            PageImpl<LoraInfo> result = new PageImpl<>(list, pageable, total);
            return new Result(Result.ReturnValue.SUCCESS, "操作成功", result);
        } catch (Exception e) {
            //  logger.error(e.getMessage(), e);
            return new Result(Result.ReturnValue.FAILURE, "系统错误");
        }
    }

    @RequestMapping(value = "/queryGpsData", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "gps数据查询", notes = "gps数据查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "integer", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "integer", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public Result queryGpsData(GPSInfo gpsInfo,@ApiIgnore Pageable pageable) throws URISyntaxException {
        try {
            Query query = new Query();
            if (StringUtils.isNotEmpty(gpsInfo.getCityID())) {
                query.addCriteria(Criteria.where("cityID").is(gpsInfo.getCityID()));
            }
//            if (StringUtils.isNotEmpty(gpsInfo.getSendTime())) {
//                query.addCriteria(Criteria.where("sendTime").gte(gpsInfo.getSendTime()));
//            }
            int total = (int) mongoTemplate.count(query, GPSInfo.class);
            query.with(pageable);
            List<GPSInfo> list = mongoTemplate.find(query, GPSInfo.class);
            PageImpl<GPSInfo> result = new PageImpl<>(list, pageable, total);
            return new Result(Result.ReturnValue.SUCCESS, "操作成功", result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new Result(Result.ReturnValue.FAILURE, "操作失败");
        }}}
